package com.app.demo.imageEdit;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.app.demo.FilterListAdapter;
import com.app.demo.HorizontalListView;
import com.app.demo.ImageEditApplication;
import com.app.demo.R;
import com.app.demo.activity.BaseActivity;
import com.app.demo.base.TitleViews;
import com.app.demo.util.FileUtils;
import com.app.demo.util.ImageUtil;
import com.app.demo.util.ViewSizeUtil;
import com.warkiz.widget.IndicatorSeekBar;
import com.warkiz.widget.OnSeekChangeListener;
import com.warkiz.widget.SeekParams;

import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import jp.co.cyberagent.android.gpuimage.GPUImage3x3ConvolutionFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageBoxBlurFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageBrightnessFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageBulgeDistortionFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageCGAColorspaceFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageColorMatrixFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageContrastFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageCrosshatchFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageDilationFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageDissolveBlendFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageEmbossFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageExposureFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageFalseColorFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageGammaFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageGaussianBlurFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageGlassSphereFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageGrayscaleFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageHazeFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageHighlightShadowFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageHueFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageKuwaharaFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageLevelsFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageMonochromeFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageOpacityFilter;
import jp.co.cyberagent.android.gpuimage.GPUImagePosterizeFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageRGBDilationFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageRGBFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageSaturationFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageSharpenFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageSketchFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageSmoothToonFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageSphereRefractionFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageSwirlFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageToneCurveFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageToonFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageView;
import jp.co.cyberagent.android.gpuimage.GPUImageVignetteFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageWhiteBalanceFilter;

/**
 *   on 17/2/9.
 */

public class ImageSaturationActivity extends BaseActivity {
    IndicatorSeekBar rangeSeekBar;
    private GPUImageFilterTools.FilterAdjuster mFilterAdjuster;
    private GPUImageView mGPUImageView;
    private ExecutorService executorService = Executors.newSingleThreadExecutor();

    private FilterListAdapter filterListAdapter;
    private ArrayList<String> filterList = new ArrayList<>();
    private String curFilter = "";

    @Override
    protected void initTitle(TitleViews titleViews) {
        titleViews.custom_title.setBackgroundResource(R.color.white);
        setStatusBarColorRes(R.color.white);
        titleViews.center_container_title_text.setText(R.string.post_image_saturation);
        titleViews.center_container_title_text.setTextColor(0xff645e66);
        titleViews.title_divider.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String url = getIntent().getStringExtra("url");
        setContentView(R.layout.activity_image_saturation);
        ViewGroup.LayoutParams layoutParams = getView(R.id.image_container).getLayoutParams();
        layoutParams.height = layoutParams.width = ViewSizeUtil.getCustomDimen(360f);
        GPUImageFilter filter = GPUImageFilterTools.createFilterForType(ImageEditApplication.getInstance(), GPUImageFilterTools.FilterType.SATURATION);
        mFilterAdjuster = new GPUImageFilterTools.FilterAdjuster(filter);
        rangeSeekBar = findViewById(R.id.rangeSeekBar);

        mGPUImageView = getView(R.id.post_image);
//        final Bitmap bitmap = ImageEditActivity.currentBitmap;
        final Bitmap bitmap = ImageUtil.getSuitBitmap(url);
        mGPUImageView.setImage(bitmap);
        mGPUImageView.setFilter(filter);
        setImageVieSize(mGPUImageView, bitmap.getWidth(), bitmap.getHeight());

        rangeSeekBar.setDecimalScale(1);


        findViewById(R.id.btn_ori).setOnClickListener(v -> {
            mGPUImageView.setFilter(new GPUImageFilter());
            filterListAdapter.setCheckItem(-1);
        });


        filterList.add("黑白");
        filterList.add("亮度");
        filterList.add("曝光");
        filterList.add("对比度");
        filterList.add("饱和度");
        filterList.add("反色");
        filterList.add("阶调曲线");
        filterList.add("灰度系数");
        filterList.add("关卡");
        filterList.add("矩阵转换颜色");
        filterList.add("RGB");
        filterList.add("色调");
        filterList.add("白平衡");
        filterList.add("阴影和高光");
        filterList.add("转换单色");
        filterList.add("指定颜色混合");
        filterList.add("雾霾");
        filterList.add("不透明度");
        filterList.add("锐化");
        filterList.add("高斯模糊");
        filterList.add("框模糊");
        filterList.add("卷积核");
        filterList.add("膨胀");
        filterList.add("膨胀2");
        filterList.add("黑白交叉");
        filterList.add("草图");
        filterList.add("卡通");
        filterList.add("降噪");
        filterList.add("浮雕");
        filterList.add("简单图象");
        filterList.add("旋转失真");
        filterList.add("凸起");
        filterList.add("折射");
        filterList.add("折射2");
        filterList.add("渐晕");
        filterList.add("抽象");
        filterList.add("模拟CGA");
        filterListAdapter = new FilterListAdapter(this, filterList);
        HorizontalListView hv = findViewById(R.id.hlv_filter);
        hv.setAdapter((filterListAdapter));
        hv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                filterListAdapter.setCheckItem(position);
                curFilter = filterList.get(position);
                if ("黑白".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageGrayscaleFilter());
                } else if ("亮度".equals(curFilter)) {
                    rangeSeekBar.setMin(-1.0f);
                    rangeSeekBar.setMax(1.0f);
                    rangeSeekBar.setProgress(0.0f);
                    rangeSeekBar.customTickTexts(new String[]{String.valueOf(rangeSeekBar.getMin()), String.valueOf(rangeSeekBar.getMax())});
                    mGPUImageView.setFilter(new GPUImageBrightnessFilter());
                } else if ("曝光".equals(curFilter)) {
                    rangeSeekBar.setMin(-10.0f);
                    rangeSeekBar.setMax(10.0f);
                    rangeSeekBar.setProgress(0.0f);
                    rangeSeekBar.customTickTexts(new String[]{String.valueOf(rangeSeekBar.getMin()), String.valueOf(rangeSeekBar.getMax())});
                    mGPUImageView.setFilter(new GPUImageExposureFilter());
                } else if ("对比度".equals(curFilter)) {
                    rangeSeekBar.setMin(0.0f);
                    rangeSeekBar.setMax(4.0f);
                    rangeSeekBar.setProgress(1.0f);
                    rangeSeekBar.customTickTexts(new String[]{String.valueOf(rangeSeekBar.getMin()), String.valueOf(rangeSeekBar.getMax())});
                    mGPUImageView.setFilter(new GPUImageContrastFilter());
                } else if ("饱和度".equals(curFilter)) {
                    rangeSeekBar.setMin(0.0f);
                    rangeSeekBar.setMax(2.0f);
                    rangeSeekBar.setProgress(1.0f);
                    rangeSeekBar.customTickTexts(new String[]{String.valueOf(rangeSeekBar.getMin()), String.valueOf(rangeSeekBar.getMax())});
                    mGPUImageView.setFilter(new GPUImageSaturationFilter());
                } else if ("阶调曲线".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageToneCurveFilter());
                } else if ("灰度系数".equals(curFilter)) {
                    rangeSeekBar.setMin(0.0f);
                    rangeSeekBar.setMax(3.0f);
                    rangeSeekBar.setProgress(1.0f);
                    rangeSeekBar.customTickTexts(new String[]{String.valueOf(rangeSeekBar.getMin()), String.valueOf(rangeSeekBar.getMax())});
                    mGPUImageView.setFilter(new GPUImageGammaFilter());
                } else if ("关卡".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageLevelsFilter());
                } else if ("矩阵转换颜色".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageColorMatrixFilter());
                } else if ("RGB".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageRGBFilter());
                } else if ("色调".equals(curFilter)) {
                    rangeSeekBar.setMin(0f);
                    rangeSeekBar.setMax(90f);
                    rangeSeekBar.setProgress(90f);
                    rangeSeekBar.customTickTexts(new String[]{String.valueOf(rangeSeekBar.getMin()), String.valueOf(rangeSeekBar.getMax())});
                    mGPUImageView.setFilter(new GPUImageHueFilter());
                } else if ("白平衡".equals(curFilter)) {
                    rangeSeekBar.setMin(4000f);
                    rangeSeekBar.setMax(7000f);
                    rangeSeekBar.setProgress(5000f);
                    rangeSeekBar.customTickTexts(new String[]{String.valueOf(rangeSeekBar.getMin()), String.valueOf(rangeSeekBar.getMax())});
                    mGPUImageView.setFilter(new GPUImageWhiteBalanceFilter());
                } else if ("阴影和高光".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageHighlightShadowFilter());
                } else if ("转换单色".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageMonochromeFilter());
                } else if ("指定颜色混合".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageFalseColorFilter());
                } else if ("雾霾".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageHazeFilter());
                } else if ("不透明度".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageOpacityFilter());
                } else if ("锐化".equals(curFilter)) {
                    rangeSeekBar.setMin(-4.0f);
                    rangeSeekBar.setMax(4.0f);
                    rangeSeekBar.setProgress(0.0f);
                    rangeSeekBar.customTickTexts(new String[]{String.valueOf(rangeSeekBar.getMin()), String.valueOf(rangeSeekBar.getMax())});
                    mGPUImageView.setFilter(new GPUImageSharpenFilter());
                } else if ("高斯模糊".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageGaussianBlurFilter(2.0f));
                } else if ("框模糊".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageBoxBlurFilter(2.0f));
                } else if ("卷积核".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImage3x3ConvolutionFilter());
                } else if ("膨胀".equals(curFilter)) {
                    rangeSeekBar.setMin(1.0f);
                    rangeSeekBar.setMax(4.0f);
                    rangeSeekBar.setProgress(1.0f);
                    rangeSeekBar.customTickTexts(new String[]{String.valueOf(rangeSeekBar.getMin()), String.valueOf(rangeSeekBar.getMax())});
                    mGPUImageView.setFilter(new GPUImageDilationFilter());
                } else if ("膨胀2".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageRGBDilationFilter());
                } else if ("黑白交叉".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageCrosshatchFilter());
                } else if ("草图".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageSketchFilter());
                } else if ("卡通".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageToonFilter());
                } else if ("降噪".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageSmoothToonFilter());
                } else if ("浮雕".equals(curFilter)) {
                    rangeSeekBar.setMin(0.0f);
                    rangeSeekBar.setMax(4.0f);
                    rangeSeekBar.setProgress(1.0f);
                    rangeSeekBar.customTickTexts(new String[]{String.valueOf(rangeSeekBar.getMin()), String.valueOf(rangeSeekBar.getMax())});
                    mGPUImageView.setFilter(new GPUImageEmbossFilter());
                } else if ("简单图象".equals(curFilter)) {
                    rangeSeekBar.setMin(1f);
                    rangeSeekBar.setMax(256f);
                    rangeSeekBar.setProgress(10f);
                    rangeSeekBar.customTickTexts(new String[]{String.valueOf(rangeSeekBar.getMin()), String.valueOf(rangeSeekBar.getMax())});
                    mGPUImageView.setFilter(new GPUImagePosterizeFilter());
                } else if ("旋转失真".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageSwirlFilter());
                } else if ("凸起".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageBulgeDistortionFilter());
                } else if ("折射".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageSphereRefractionFilter());
                } else if ("折射2".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageGlassSphereFilter());
                } else if ("渐晕".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageVignetteFilter());
                } else if ("抽象".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageKuwaharaFilter());
                } else if ("模拟CGA".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageCGAColorspaceFilter());
                }


            }
        });


        rangeSeekBar.setOnSeekChangeListener(new OnSeekChangeListener() {
            @Override
            public void onSeeking(SeekParams p) {
                if ("亮度".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageBrightnessFilter(p.progressFloat));
                } else if ("曝光".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageExposureFilter(p.progressFloat));
                } else if ("对比度".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageContrastFilter(p.progressFloat));
                } else if ("饱和度".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageSaturationFilter(p.progressFloat));
                } else if ("灰度系数".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageGammaFilter(p.progressFloat));
                } else if ("色调".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageHueFilter(p.progressFloat));
                } else if ("白平衡".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageWhiteBalanceFilter(p.progressFloat, 0.0f));
                } else if ("不透明度".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageOpacityFilter(p.progressFloat));
                } else if ("锐化".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageSharpenFilter(p.progressFloat));
                } else if ("膨胀".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageDilationFilter(p.progress));
                } else if ("混合".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageDissolveBlendFilter(p.progressFloat));
                }  else if ("浮雕".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImageEmbossFilter(p.progressFloat));
                } else if ("简单图象".equals(curFilter)) {
                    mGPUImageView.setFilter(new GPUImagePosterizeFilter(p.progress));
                }
            }

            @Override
            public void onStartTrackingTouch(IndicatorSeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(IndicatorSeekBar seekBar) {

            }
        });

        setOnClickListener(R.id.activity_image_saturation_cancel, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        setOnClickListener(R.id.activity_image_saturation_confirm, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showWaitDialog();
                executorService.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            String saveClip = ImageUtil.saveClip(mGPUImageView.capture(bitmap.getWidth(), bitmap.getHeight()), FileUtils.File_TEMP_CLIP);
//                            ImageEditActivity.currentBitmap = ImageEditActivity.originalBitmap = mGPUImageView.capture(ImageEditActivity.currentBitmap.getWidth(), ImageEditActivity.currentBitmap.getHeight());
                            ImageEditApplication.getInstance().handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    Intent intent = new Intent();
                                    intent.putExtra("url",saveClip);
                                    setResult(RESULT_OK, intent);
                                    finish();
                                    hideWaitDialog();
                                }
                            });
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                });

            }
        });
    }

    public static void startImageSaturationActivity(BaseActivity context,String url, int requestCode) {
        Intent intent = new Intent(context, ImageSaturationActivity.class);
        intent.putExtra("url",url);
        context.startActivityForResult(intent, requestCode);
    }

    private void setImageVieSize(View view, int width, int height) {
        int fixSize = ViewSizeUtil.getCustomDimen(360f);
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if (width == height) {
            width = height = fixSize;
        } else {
            if (width > height) {
                height = height * fixSize / width;
                width = fixSize;
            } else {
                width = width * fixSize / height;
                height = fixSize;
            }
        }
        layoutParams.height = height;
        layoutParams.width = width;
    }
}
